package com.anosine.gpsl.android.impl;

import android.os.Build;

import com.anosine.gpsl.android.AppConstants;
import com.anosine.gpsl.android.WebConstants;
import com.anosine.gpsl.android.dao.LoginDao;
import com.anosine.gpsl.android.dao.ValidateDao;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.presenter.BasePresenterActivity.OnLoadListener;
import com.anosine.gpsl.android.util.HttpRequestListener;
import com.anosine.gpsl.android.util.SendHttpUtils;
import com.anosine.gpsl.android.util.ToastHelper;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;

/**
 * @Title: ValidateDaoImpl
 * @Package com.anosine.gpsl.android.impl
 * @Description: 接口实现
 * @author: sunny
 * @date: 2015-11-19
 * @version V1.0
 */
public class ValidateDaoImpl implements ValidateDao {
	private OnLoadListener loadListener;

	/**
	 * @Title: ValidateDaoImpl(OnLoadListener loadListener)
	 * @Description: 构造方法，初始化对象，给loadListener赋值
	 * @param: loadListener
	 * @throws
	 */

	public ValidateDaoImpl(OnLoadListener loadListener) {
		super();
		this.loadListener = loadListener;
	}

	/**
	 * @Title: getValidate(String username,String imeino, String realname,String
	 *         ChannelId,String idno, String imsino,String mobilebrank,String
	 *         isAndroid,String baidupushid ,String mobilemodel)
	 * @Description: 调用验证登录接口
	 * @param username
	 *            用户名
	 * @param realname
	 *            真实姓名
	 * @param imeino
	 *            IMEI号
	 * @param imsino
	 *            IMSI号
	 * @param mobilebrank
	 *            手机品牌
	 * @param mobilemodel
	 *            手机型号
	 * @param isAndroid
	 *            手机系统，1为android
	 * @param ChannelId
	 *            百度推送手机端唯一标识
	 * @param baidupushid
	 *            百度推送ID
	 * @param idno
	 *            身份证后八位
	 * @return: 无
	 * @throws
	 */

	@Override
	public void getValidate(String username, String imeino, String realname,
			String ChannelId, String idno, String imsino, String mobilebrank,
			String isAndroid, String baidupushid, String mobilemodel) {

		RequestParams params = new RequestParams();
		params.addBodyParameter("userinfo.username", username);
		params.addBodyParameter("userinfo.IMEI", imeino);
		params.addBodyParameter("mobile.realname", realname);
		params.addBodyParameter("mobile.ChannelId", ChannelId);
		params.addBodyParameter("mobile.idno", idno);
		params.addBodyParameter("mobile.imsino", imsino);
		params.addBodyParameter("mobile.mobilebrank", mobilebrank);
		params.addBodyParameter("mobile.isAndroid", isAndroid);
		params.addBodyParameter("mobile.baidupushid", baidupushid);
		params.addBodyParameter("mobile.mobilemodel", mobilemodel);
		SendHttpUtils sendHttp = new SendHttpUtils();
		TypeToken type = new TypeToken<Login>() {
		};
		String url = String.format("%1s/%2s", AppConstants.sCommonUrl,
				WebConstants.Do_CheckLogin_Action);
		sendHttp.doRequest(url, params, type, new HttpRequestListener<Login>() {
			/**
			 * @Title: onSuccess(String result, Login t)
			 * @Description: 解析成功做的操作
			 * @param: result json
			 * @param: t 登录的实体类
			 * @return: 无
			 * @throws
			 */
			@Override
			public void onSuccess(String result, Login t) {

				loadListener.onSuccess(t);

			}

			/**
			 * @Title: onFailure(String error)
			 * @Description: 解析失败做的操作
			 * @param: error 错误信息
			 * @return: 无
			 * @throws
			 */
			@Override
			public void onFailure(String error) {
				loadListener.onFailure();
				ToastHelper.toast(error);
			}
		});
	}
}
